package com.lkm.app_ui.m.Imple.fun;

import android.content.Context;

import com.lkm.app.event.OnNetworkStateEvent;
import com.lkm.app_ui.AppConfig;
import com.lkm.app_ui.AppConst;
import com.lkm.app_ui.RunTimeData;
import com.lkm.app_ui.netapi.AppApi;
import com.lkm.comlib.fun_m.NetworkStateModule;
import com.lkm.comlib.task2.DefaultTaskExecutor;
import com.lkm.frame.entity.ResponEntityAbs;
import com.lkm.frame.file.FileHelper;
import com.lkm.frame.otto.LinkBus;
import com.lkm.frame.otto.ObserverAutoNext;
import com.lkm.frame.task2.Task;
import com.lkm.frame.task2.TaskAdapter;
import com.lkm.market2_0.entity.ResponEntity;

import java.io.File;



class HoldPrintTemplateTask extends TaskAdapter<Void, String, ResponEntityAbs<String>> {

    public static Task<Void, String, ResponEntityAbs<String>> start() {
        HoldPrintTemplateTask task = new HoldPrintTemplateTask(true);
        task.action();
        task.setParam(null);
        task.execTask();
        return task;
    }

    private boolean isHold;

    public HoldPrintTemplateTask(boolean isHold) {
        super(new DefaultTaskExecutor(AppConst.getTaskExecutor(), true), HoldPrintTemplateTask.class.getName(), AppConst.getTaskManager2());
        this.isHold = isHold;
    }

    @Override
    public void doExecuting(Void param) {
        super.doExecuting(param);
        mTaskExecutor.executor(new Runnable() {
            @Override
            public void run() {
                ayncdoExecuting();
            }
        });
    }

    private void ayncdoExecuting() {
        String json = AppApi.loadPrintTemplate(HoldPrintTemplateTask.this) ;

        ResponEntity<String> responEntity = ResponEntity.fromJson(json, String.class, "");

        if (responEntity.isSuccess()) {

            AppConst.getApplication().getSharedPreferences("settingapp", Context.MODE_PRIVATE).edit().putString("orderPrintTemplate", responEntity.getData()).commit();
            complete(responEntity);

        } else {

            if (isHold && json == null) {
                requestRe();
            } else {
                complete(new ResponEntity<String>().fail(null, responEntity.getMsg()));
            }

        }

    }
    @Override
    public void onFinish(ResponEntityAbs<String> aBoolean) {
        super.onFinish(aBoolean);
        if(aBoolean.isSuccess()){
            RunTimeData.orderPrintTemplate = aBoolean.getData();
        }
    }


    int isReForNetOK = 0;

    private void requestRe() {
        if (!getIsContinue()) return;

        if (isReForNetOK >= 5) {
            complete(new ResponEntity<String>().fail(null, ""));
            return;
        }

        if (NetworkStateModule.getInstance().getIsNetworkOK()) {
            isReForNetOK++;
            doExecuting(null);
        } else {

            if (observerAutoNext != null)
                LinkBus.unregisterReceiver(observerAutoNext);
            LinkBus.registerReceiver(getobserverAutoNext());
        }
    }

    ObserverAutoNext observerAutoNext;

    private ObserverAutoNext getobserverAutoNext() {
        if (observerAutoNext == null) {
            observerAutoNext = new ObserverAutoNext<OnNetworkStateEvent>() {
                @Override
                public void onReceiver(OnNetworkStateEvent data) {
                    if (!getIsContinue()) {
                        LinkBus.unregisterReceiver(this);
                        return;
                    }
                    if (data.extIsNetworkOK) {
                        LinkBus.unregisterReceiver(this);
                        doExecuting(null);
                    }
                }

                @Override
                public Class getBusEventClass() {
                    return OnNetworkStateEvent.class;
                }
            };
        }
        return observerAutoNext;
    }

    @Override
    public boolean onInterceptEnd(boolean isCancel, ResponEntityAbs<String> result) {
        if (observerAutoNext != null)
            LinkBus.unregisterReceiver(observerAutoNext);
        return super.onInterceptEnd(isCancel, result);
    }



}
